package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;


import com.xumt.hwHMSdemo.cannectSb.HttpRequest;
import com.xumt.hwHMSdemo.ui.ShopingFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommodityActivity extends AppCompatActivity {
    public  static String clickname=null;
    public  static String clickprice=null;
    public  static String clickid=null;
    public  static String clickdescription=null;
    public  static String clickimage=null;
    public  static String clickfenlei=null;
    public  static int position=0;
    private ArrayList data_list = new ArrayList<String>();
    private ArrayList data_list1 = new ArrayList<String>();
    private String name;
    private String commoditylistresult;
    private SimpleAdapter commodityadapter = null;
    public  static List<Map<String, Object>> commoditylist = new ArrayList<Map<String, Object>>();
    public  static List<Map<String, Object>> startlist = new ArrayList<Map<String, Object>>();
    private Spinner spinner1;
    private ArrayAdapter<String> arr_adapter1;
    private Spinner spinner2;
    private ArrayAdapter<String> arr_adapter2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity);
        commoditylist.clear();
        startlist.clear();
        findname();
        getlist();
        TextView textView=(TextView)findViewById(R.id.shopname1);
        TextView textView1=(TextView)findViewById(R.id.shopplace1);
        textView.setText(ShopingFragment.clickshopname);
        textView1.setText(ShopingFragment.clickshopplace);
        spinner1 = (Spinner) findViewById(R.id.fenlei);
        spinner2 = (Spinner) findViewById(R.id.paixu);
        ListView listView = (ListView) findViewById(R.id.list_view);
        Button button=(Button)findViewById(R.id.road);
        Button button1=(Button)findViewById(R.id.bzt);
       Intent intent1=new Intent(CommodityActivity.this,Commodity_message.class);
        Intent intent=new Intent(CommodityActivity.this,MapDetailActivity.class);
        Intent intent2=new Intent(CommodityActivity.this,bztActivity.class);
        arr_adapter1= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list);
        arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(arr_adapter1);
        arr_adapter2= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list1);
        arr_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(arr_adapter2);
        spinner1.setSelection(0, true);
        spinner2.setSelection(0, true);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
                commoditylist.clear();
                if(position==0) {
                    for (int i = 0; i < startlist.size(); i++) {
                        Map opMap = (Map) startlist.get(i);
                        //map中存在此id，将数据存放当前key的map中
                        commoditylist.add(opMap);
                    }
                }

            if(position==1) {
                for (int i = 0; i < startlist.size(); i++) {
                    Map opMap = (Map) startlist.get(i);
                    //map中存在此id，将数据存放当前key的map中
                    String fenlei=(String) opMap.get("classification");
                    if (fenlei.equals("宠物饲料")) {
                        commoditylist.add(opMap);
                    }
                }
            }

                if(position==2) {
                    for (int i = 0; i < startlist.size(); i++) {
                        Map opMap = (Map) startlist.get(i);
                        //map中存在此id，将数据存放当前key的map中
                        String fenlei=(String) opMap.get("classification");
                        if (fenlei.equals("宠物玩具")) {
                            commoditylist.add(opMap);
                        }
                    }
                }

                if(position==3) {
                    for (int i = 0; i < startlist.size(); i++) {
                        Map opMap = (Map) startlist.get(i);
                        //map中存在此id，将数据存放当前key的map中
                        String fenlei=(String) opMap.get("classification");
                        if (fenlei.equals("宠物用品")) {
                            commoditylist.add(opMap);
                        }
                    }
                }
                spinner2.setSelection(0, true);
                commodityadapter.notifyDataSetChanged();//通知SimpleAdapter数据改变

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {

               if(position==1)
               {
               sortRev(commoditylist,"commodityprice");
               }
                if(position==2)
                {
                    sort(commoditylist,"commodityprice");
                }
                commodityadapter.notifyDataSetChanged();//通知SimpleAdapter数据改变

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });


        commodityadapter = new CommodityActivity.MySimpleAdapter(this, getData(), R.layout.commoditylist, new String[]{"commodityname", "classification", "commodityprice","commodityimage1"},
                new int[]{R.id.commodityname, R.id.classification, R.id.commodityprice,R.id.commodityimage});

        listView.setAdapter(commodityadapter);
        AdapterView.OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Map<String, Object>map=commoditylist.get(position);
//                clickname=(String) map.get("commodityname");
//                clickdescription=(String) map.get("commoditymessage");
//                clickfenlei=(String) map.get("classification");
//               clickid=(String) map.get("commodityid");
//                clickprice=(String) map.get("commodityprice");
//                clickimage=(String) map.get("commodityimage");
//                map.put("commodityname",  jsonObject.getString("name"));
//                map.put("commodityprice", jsonObject.getString("price"));
//                map.put("commoditymessage", jsonObject.getString("description"));
//                map.put("classification", jsonObject.getString("classification"));
//                map.put("commodityimage", jsonObject.getString("image"));
//                map.put("commodityid",  jsonObject.getInt("id"));
                CommodityActivity.position=position;
                Log.i("HHHH", clickname+" "+clickimage+" "+clickprice+" "+clickfenlei+" "+clickdescription);
                startActivity(intent1);
            }
        };
        listView.setOnItemClickListener(itemClickListener);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent2);
            }
        });

    }

    private class MySimpleAdapter extends SimpleAdapter {

        public MySimpleAdapter(Context context,
                               List<? extends Map<String, ?>> data, int resource,
                               String[] from, int[] to) {
            super(context, data, resource, from, to);
            // TODO Auto-generated constructor stub
        }
    }

    public void findname()
    {

        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                    commoditylistresult=request.sendGet("http://169.254.67.221:8081/Commdity/getcommditylist","shopid="+ShopingFragment.clickshop);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void getlist()
    {

        data_list.add("全部商品");
        data_list.add("宠物饲料");
        data_list.add("宠物玩具");
        data_list.add("宠物用品");
        data_list1.add("默认排序");
        data_list1.add("按价格升序");
        data_list1.add("按价格降序");

    }

    private List<Map<String, Object>> getData() {
        try {
            findname();
            Thread.sleep(500);
            JSONArray jsonArray = new JSONArray(commoditylistresult);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("commodityname",  jsonObject.getString("name"));
                map.put("commodityprice", jsonObject.getString("price"));
                map.put("commoditymessage", jsonObject.getString("description"));
                map.put("classification", jsonObject.getString("classification"));
                map.put("commodityimage", jsonObject.getString("image"));
                map.put("commodityid",  jsonObject.getInt("id"));
                map.put("stock",  jsonObject.getInt("stock"));
                map.put("xiaoliang",  jsonObject.getInt("xiaoliang"));
                Context ctx=getBaseContext();
                int resId = getResources().getIdentifier(jsonObject.getString("image"), "drawable" ,ctx.getPackageName());
                map.put("commodityimage1",resId);
                commoditylist.add(map);
                startlist.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return commoditylist;
    }

    public static void sort( List<Map<String, Object>> list , final String args ) {

            Collections.sort(list, new Comparator<Map<String,Object>>() {
                public int compare(Map<String,Object> mapOne, Map<String,Object> mapTwo) {
                    int flag = 0 ;
                    int  first = Integer.valueOf(mapOne.get(args).toString());
                    int  second = Integer.valueOf(mapTwo.get(args).toString());

                    if((first - second) > 0){
                        flag = -1 ;
                    }else if((first - second) < 0){
                        flag = 1 ;
                    }else{
                        flag = 0 ;
                    }
                    return flag ;
                };
            });

    }

    /**
     * @Description: 按照 args 由小到大排序
     * @param list
     * @param args
     * @return void
     * @author beyond
     * @date 2015-8-12
     */
    public static void sortRev( List<Map<String, Object>> list , final String args ) {

            Collections.sort(list, new Comparator<Map<String,Object>>() {
                public int compare(Map<String,Object> mapOne, Map<String,Object> mapTwo) {
                    int flag = 0 ;
                    int  first = Integer.valueOf(mapOne.get(args).toString());
                    int  second = Integer.valueOf(mapTwo.get(args).toString());

                    if((first - second) > 0){
                        flag = 1 ;
                    }else if((first - second) < 0){
                        flag = -1 ;
                    }else{
                        flag = 0 ;
                    }
                    return flag ;
                };
            });

    }
}